package com.young.mylib.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * <pre>
 *     author: young
 *     time  : 2016/9/27
 *     desc  : 崩溃相关工具类
 * </pre>
 */
public class Log4AUtils {
    private final static String TAG = "LogUtils";
    private volatile static Log4AUtils mInstance;
    private static Context context;
    private UncaughtExceptionHandler mHandler;

    private boolean mInitialized;
    private static String crashDir;
    private String versionName;
    private int versionCode;
    // 用于格式化日期,作为日志文件名的一部分
    private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    public Log4AUtils(Context context) {
        this.context = context;
    }


    /**
     * 初始化
     *
     * @return {@code true}: 成功<br>{@code false}: 失败
     */
    public boolean init() {
        if (mInitialized) return true;

        if (Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED)) {
            crashDir = FileUtils.getAppPath(context) + "log/";

        } else {
            File baseCache = context.getCacheDir();
            if (baseCache == null) return false;
            crashDir = baseCache.getPath() + File.separator + "log" + File.separator;

        }
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            versionCode = pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }

        return mInitialized = true;
    }

    public static void info(String str) {

        String now = new SimpleDateFormat("yy-MM-dd-HH", Locale.getDefault()).format(new Date());
        String time = new SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
        final String fullPath = crashDir + now + ".txt";
        JSONObject json = new JSONObject();
        json.put("time", time);
        json.put("log", str);
        Log.i("Log4AUtils","write log: "+json.toJSONString());
        FileUtils.writeFile(fullPath, json.toJSONString()+",\r\n", FileUtils.isExist(fullPath));

    }

    public static String writeFile(String fullPath, String sb) {
        try {
            if (hasSdcard()) {
                Log.i(TAG, fullPath);
                if (!FileUtils.createOrExistsFile(fullPath)) return "connot create crash dir";
                FileOutputStream fos = new FileOutputStream(fullPath, true);
                fos.write(sb.getBytes());
                fos.flush();
                fos.close();
                return fullPath;
            }
        } catch (Exception e) {

        }
        Log.e(TAG, sb);

        return "hasn`t sdcard";
    }

    public static boolean hasSdcard() {
        String status = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(status);
    }

    private boolean handleException(final Throwable ex) {
        if (ex == null) {
            Log.e(TAG, "EX IS NULL");
            return false;
        }
        try {
            // 使用Toast来显示异常信息
            new Thread() {

                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(context, "Sorry,Some mistakes happened!",
                            Toast.LENGTH_LONG).show();
                    if (ex != null && ex.getMessage() != null) {
                        Log.e(TAG, ex.getMessage());
                    } else {
                        Log.e(TAG, "程序出现异常 ex is null");
                    }
                    Looper.loop();
                }
            }.start();
            // 收集设备参数信息

            SystemClock.sleep(3000);
        } catch (Exception e) {
            Log.e(TAG, "handleException: " + e.getMessage());
            e.printStackTrace();
        }
        Log.e(TAG, "handleException: return true");
        return true;
    }

    /**
     * 获取崩溃头
     *
     * @return 崩溃头
     */
    private String getCrashHead() {
        return "\n************* Crash Log Head ****************" +
                "\nDevice Manufacturer: " + Build.MANUFACTURER +// 设备厂商
                "\nDevice Model       : " + Build.MODEL +// 设备型号
                "\nAndroid Version    : " + Build.VERSION.RELEASE +// 系统版本
                "\nAndroid SDK        : " + Build.VERSION.SDK_INT +// SDK版本
                "\nApp VersionName    : " + versionName +
                "\nApp VersionCode    : " + versionCode +
                "\n************* Crash Log Head ****************\n\n";
    }

    /**
     * 关闭IO
     *
     * @param closeables closeable
     */
    public static void closeIO(Closeable... closeables) {
        if (closeables == null) return;
        for (Closeable closeable : closeables) {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
